﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Drawing;
using System.Data;

public partial class Reports_BI_Dynamic_CashAllShops_Month : PageBase
{

    #region Members

    DataTable tbl;
    Ko2.DB.BI.Reports.Cash report = new Ko2.DB.BI.Reports.Cash();
    DateTime dt;

    #endregion

    protected override void OnPreInit(EventArgs e)
    {

        base.OnPreInit(e);

        dt = DateTime.Now;
        dt = new DateTime(dt.Year, dt.Month, dt.Day);

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        Exception err = null;

        if (!IsPostBack)
        {
            for (int i = 2008; i <= dt.Year; i++)
            {
                for (int ii = 1; ii <= 12; ii++)
                {
                    string strTmp = String.Format("{0:00}-{1:0000}", ii, i);
                    oDate.Items.Insert(0, strTmp);
                }
            }
            string dtStrNow = String.Format("{0:00}-{1:0000}", dt.Month, dt.Year);
            // oDate.SelectedIndex = 0;// oDate.Items.Count - 1;
            oDate.SelectedValue = dtStrNow;
            oFormat.SelectedValue = "2";
        }
        else
        {

            string str = oDate.SelectedValue;
            if (!String.IsNullOrEmpty(str))
            {
                string str1 = str.Substring(0, 2);
                string str2 = str.Substring(3, 4);
                int Month = (int)Convert.ToInt32(str1);
                int Year = (int)Convert.ToInt32(str2);
                dt = new DateTime(Year, Month, 1);
                dt = dt.AddMonths(1);
                dt = dt.AddDays(-1);
            }

        }

        //___________
        // Get data :
        tbl = new DataTable("tblSrc");
        Ko2.DB.BI.Reports.Cash.Cube cubeType;
        if (oCubeList.SelectedIndex == 0)
            cubeType = Ko2.DB.BI.Reports.Cash.Cube.KPI;
        else
            cubeType = Ko2.DB.BI.Reports.Cash.Cube.Продажи;
        err = report.Get(ref tbl, dt, cubeType);
        // Resume :
        if (err != null)
        {
            Response.Write("Ошибка : " + err.Message);
            gv.Visible = false;
            return;
        }
        gv.Visible = true;
        gv.DataSource = tbl;
        gv.DataBind();

    }

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int i = -1;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (TableCell c in e.Row.Cells)
            {
                i++;

                if (e.Row.Cells[0].Text != "Все")
                {
                    e.Row.Cells[0].BackColor = Color.FromArgb(0, 165, 203, 247);
                    e.Row.Cells[0].ForeColor = Color.Black;
                    e.Row.Cells[0].Font.Bold = true;
                    e.Row.Cells[0].Font.Size = FontUnit.XSmall;
                    e.Row.Cells[0].Font.Name = "Verdana";
                }

                if (i != 0)
                {
                    c.HorizontalAlign = HorizontalAlign.Right;
                    c.Font.Name = "Verdana";
                }

                if (i == 0 && e.Row.Cells[i].Text == "Все")
                {
                    e.Row.BackColor = Color.FromArgb(0, 255, 255, 210);
                    e.Row.ForeColor = Color.Black;
                }
                else
                    e.Row.Cells[0].ForeColor = Color.Black;

                if (i == 1)
                {
                    e.Row.Cells[1].BackColor = Color.FromArgb(0, 255, 255, 210);
                    e.Row.Cells[1].ForeColor = Color.Black;
                }

                if (i > 0)
                {
                    string str = e.Row.Cells[i].Text;
                    str = HttpUtility.HtmlDecode(str);
                    str = str.Trim();
                    float val = 0;
                    if (!String.IsNullOrEmpty(str))
                    {
                        val = (float)Convert.ToDecimal(str);
                        e.Row.Cells[i].Text = ReturnNumericStringFormat(val);
                    }
                    else
                        e.Row.Cells[i].Text = ReturnNumericStringFormat(val);
                }
            }
        }
        if (e.Row.RowIndex == -1)
        {
        }
    }

    public string ReturnNumericStringFormat(float val)
    {
        if (val == 0)
            return "";
        string str = "";
        if (oFormat.SelectedValue == "1")
            str = val.ToString("C");
        if (oFormat.SelectedValue == "2" || oFormat.SelectedValue == "3" || oFormat.SelectedValue == "4")
            str = val.ToString("C");
        if (oFormat.SelectedValue == "1")
            str = str.Replace(" ", "");
        if (oFormat.SelectedValue != "4")
            str = str.Replace("р.", "");
        if (str.Length > 3 && oFormat.SelectedValue == "1")
        {
            str = str.Substring(0, str.Length - 3);
        }
        if (str.Length > 3 && oFormat.SelectedValue == "2")
        {
            str = str.Substring(0, str.Length - 3);
        }
        str = HttpUtility.HtmlEncode(str);
        return str;
    }

}
